/*****************************************************************************************
 **  Author:jxx 2022
 **  QQ:283591387
 **完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
 **常用示例见：http://v2.volcore.xyz/document/vueDev
 **后台操作见：http://v2.volcore.xyz/document/netCoreDev
 *****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import modelBody from "../../customerinfo/q_client/client.vue";
import gridHeader from "@/components/print/selectTemplet.vue"
import {
  mapMutations
} from "vuex";
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: modelBody,
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: {
    view: [],
    box: [],
    detail: []
  }, //扩展的按钮
  methods: {
    ...mapMutations(
      ("salesContractMX", ["storeRemoveForm"])
    ),
    //下面这些方法可以保留也可以删除
    async onInit() {
      this.queryFields = ['Item_name', 'CreateDate']
      if (this.$route.path != "/q_goods_info") {

        // this.searchFormFields.AuditStatus = "1"

        this.searchFormOptions.forEach(item => {
          item.forEach(x => {
            if (x.field == "Item_brandname") {
              x.onChange = (v) => {
                if (v == "") {
                  this.storeRemoveForm()
                }
              }
            }
          })
        })
      }
      let startTime = await this.base.getOneWeekAgoDate()
      let endTime = await this.base.getDate(false) + " 23:59:59"
      this.searchFormFields.CreateDate = [startTime, endTime]

      if (this.$route.path == "/q_goods_info" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // const selectList = ['Item_clsno', 'Item_brandname', 'Display_flag', 'Supplier', 'Cost_accounting', 'AuditStatus', 'CreateDate',]
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     if (selectList.includes(x.field)) {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        // this.searchFormFields.Item_sub = null
        // this.searchFormFields.Item_name = null
        // this.searchFormFields.Item_clsno = null
        // this.searchFormFields.Item_brandname = null
        // this.searchFormFields.Item_subno = null
        // this.searchFormFields.Display_flag = null
        // this.searchFormFields.Supplier = null
        // this.searchFormFields.Cost_accounting = null
        // this.searchFormFields.AuditStatus = null
        // this.searchFormFields.CreateDate = [null, null]

        setTimeout(() => {
          this.refresh()
        })
      }
    },
    onInited() {
      if (this.$route.path == "/q_goods_inventory") {
        this.single = true
      }
      // if (this.$route.path == "/q_sales_contract"
      //   || this.$route.path == "/q_purchase_application"
      //   || this.$route.path == "/q_stock_in"
      //   || this.$route.path == "/q_warehouse_location"
      //   || this.$route.path == "/q_overflow_voucher"
      //   || this.$route.path == "/q_goods_fittings"
      // ) {
      //   this.searchFormFields.AuditStatus = "1"
      // }
      // this.setFiexdSearchForm(true);
      // this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })
      if (this.$route.path == "/q_sales_contract") {
        this.columns.forEach(item => {
          if (
            item.field == "Item_sub" || item.field == "Item_name" || item.field == "Item_clsno" || item.field == "Item_brand" || item.field == "Item_brandname" ||
            item.field == "Unit_no" || item.field == "Item_size" || item.field == "Item_subno" || item.field == "Display_flag" || item.field == "Supplier" ||
            item.field == "Cost_accounting" || item.field == "Googs_image" || item.field == "Remark" || item.field == "AuditStatus" || item.field == "Creator"
          ) {
            item.hidden = false
          } else {
            item.hidden = true
          }
        })
      }
      if (this.$route.path != "/q_goods_info") {
        this.columns.forEach(item => {
          if (item.field == "Item_name") {
            item.link = false
          }
          if (
            item.field == "Item_sub" || item.field == "Item_name" || item.field == "Item_clsno" || item.field == "Item_brand" || item.field == "Item_brandname" ||
            item.field == "Unit_no" || item.field == "Item_size" || item.field == "Item_subno" || item.field == "Display_flag" || item.field == "Supplier" ||
            item.field == "Cost_accounting" || item.field == "Googs_image" || item.field == "Remark" || item.field == "AuditStatus" || item.field == "Creator"
          ) {
            item.hidden = false
          } else {
            item.hidden = true
          }
        })
        this.setFiexdSearchForm(true);
        this.height = 550 - 150;
        this.buttons.forEach(item => {
          if (item.value != "Search") {
            item.hidden = true
          }
        })
      }
    },
    async searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if (this.$route.path != "/q_goods_info") {
        param.wheres = param.wheres.filter(item => item.name != "Item_id")
        this.$route.query = {}
      }
      if (this.$route.path == "/q_goods_info") {
        // this.storeRemoveForm()
      }
      let form = this.$store.state.salesContractMX.form
      if (this.$route.path == "/q_goods_fittings" && param.wheres.length == 1 && param.wheres[0].name == "AuditStatus" && form) {
        //改为默认查询的字段
        if (form.Item_brandname) {
          param.wheres.push({
            name: "Item_brandname",
            value: form.Item_brandname,
            displayType: "select"
          })
        }
      }

      if (this.$route.path == "/q_goods_info" && JSON.stringify(this.$route.query) != '{}') {
        await this.base.setSearchBefore(this, "Item_id", param)
        // const sectionState = ["[null,null]",JSON.stringify(["",null]),JSON.stringify([null,""]),JSON.stringify(["",""])]
        // const dateState=sectionState.includes(JSON.stringify(this.searchFormFields.CreateDate))
        // if (this.searchFormFields.Item_sub || this.searchFormFields.Item_name ||  this.searchFormFields.Item_subno||!dateState) {
        //   param.wheres=param.wheres.filter(item=>item.name!="Item_id")
        //   this.$route.query = {}
        // }
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      // console.log("result",result)
      const keyArr=[]
      result.forEach(item=>{
        for (const key in item) {
          if(item[key]=== null){
            keyArr.push(key)
          }
        }
      })
      
      const grouped = keyArr.reduce((acc, item) => {
        if (!acc[item]) {
          acc[item] = [];
        }
        acc[item].push(item);
        return acc;
      }, {});
      const hiddenKey=[]
      for (const key in grouped) {
        if(grouped[key].length==result.length){
          hiddenKey.push(key)
        }
      }
      this.columns.forEach(item => {
        if (hiddenKey.includes(item.field)) {
          item.hidden = true
        }
        if(item.field=="Supplier"&&item.hidden){
          item.link = false
          this.searchFormFields.Supplier=null
          this.searchFormOptions.forEach(item=>{
            item.forEach(x=>{
              if(x.field=="Supplier"){
                x.hidden=true
              }
            })
          })
        }
      })
      return true;
    },
    addBeforeAsync(formData) {
      this.rowAuditStatus = 0
      return this.testAdd(formData);
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.testAdd(formData);
    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let auditStatus = rows.some(x => {
        return x.AuditStatus == 1
      });
      if (auditStatus) {
        this.base.ElMessageFn('不支持删除审核通过数据')
        return false;
      }
      return true;
    },
    rowClick({
      row,
      column,
      event
    }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row);
    },
    onModelClose(iconClick) {
      //iconClick=true为点击左中上角X触发的关闭事件
      //如果返回 false不会关闭弹出框 
      //return false;
      this.storeRemoveForm()
      this.boxModel = false;
    },
    async modelOpenBefore(row) {
      // this.initDicKeys() //刷新字典
      // methods.
      // console.log("row.CreateID",row.CreateID);
      // console.log("this.$store.state.userID",this.$store.state.userID);
      this.CreateUserBoole = row.CreateID == this.$store.state.userID
      this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
    },
    modelOpenAfter(row) {
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          // if (this.$route.path != "/q_goods_info"  || !this.CreateUserBoole) { //||  this.rowAuditStatus!=0|| this.rowAuditStatus != 0
          if (this.$route.path != "/q_goods_info") {
            x.disabled = true
            this.boxButtons[0].hidden = true
            if (x.field == "Supplier") {
              x.extra = null
            }
          } else {
            x.disabled = false
            this.boxButtons[0].hidden = false
            // 去掉选择供应商
            // if (x.field == "Supplier") {
            //   x.disabled = true
            //   x.extra = {
            //     icon: "el-icon-zoom-in",
            //     text: "选择供应商",
            //     style: "color:#3a8ee6;font-size:13px;cursor:default",
            //     click: () => {
            //       this.$refs.modelBody.open()
            //     }
            //   }
            // }
          }
          if (x.field == "Item_sub") {
            x.disabled = true
          }
        })
      })

    },
    testAdd(formData) {
      if (!this.editFormFields.Abc) {
        this.base.ElMessageFn('请选择ABC级')
        return false
      }
      if (!this.editFormFields.Price_in) {
        this.base.ElMessageFn('请填写采购价')
        return false
      }
      if (!(+this.editFormFields.Price_in >= 0 && this.editFormFields.Price_in >= 0 && "" + this.editFormFields.Price_in.trim() != "")) {
        this.base.ElMessageFn('采购价请填写有效正数')
        return false
      }
      return true
    },
  }
};
export default extension;